package com.padakeji.android.pworks.content;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.impl.ext.LruDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.padakeji.android.pworks.R;

/**
 * Created by AtanL on 2015/9/14.
 */
public class UniversalImageLoaderWrapper implements PadaImageLoader {

    private Context mContext;
    private ImageLoader imageLoader;

    public UniversalImageLoaderWrapper(Context context) {
        this.mContext = context;
        imageLoader = ImageLoader.getInstance();
        configureImageLoader();
    }

    private void configureImageLoader() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_launcher)
                .showImageOnFail(R.drawable.ic_launcher)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(mContext)
                .defaultDisplayImageOptions(options)
                .denyCacheImageMultipleSizesInMemory()
                .build();

        imageLoader.init(imageLoaderConfiguration);
    }

    /**
     *
     * @param uri
     * @param imageView
     * @param options
     * @param listener
     * @param progressListener
     */
    public void display(String uri, ImageView imageView, DisplayImageOptions options, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {
        imageLoader.displayImage(uri, imageView, options, listener, progressListener);
    }

    /**
     * Uri 可以是下面这几种格式。
     *
     * String imageUri = "http://site.com/image.png";      // from Web
     I String imageUri = "file:///mnt/sdcard/image.png";   // from SD card
     * String imageUri = "content://media/external/audio/albumart/13"; // from content provider
     * String imageUri = "assets://image.png";             // from assets
     * String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)
     *
     * @param uri
     * @param imageView
     */
    @Override
    public void display(String uri, ImageView imageView) {
        //显示图片的配置
        display(uri, imageView, null, null, null);
    }

}
